package com.qike.huadongexcel.controller;

import com.qike.huadongexcel.config.PassToken;
import com.qike.huadongexcel.dto.AddProjectDto;
import com.qike.huadongexcel.dto.DelProjectDto;
import com.qike.huadongexcel.dto.LoginDto;
import com.qike.huadongexcel.dto.ResultSave;
import com.qike.huadongexcel.response.BaseResult;
import com.qike.huadongexcel.response.RetResponse;
import com.qike.huadongexcel.service.ExcelService;
import com.qike.huadongexcel.vo.DBFileListVo;
import com.qike.huadongexcel.vo.LoginVo;
import com.qike.huadongexcel.vo.ProjectListVo;
import com.qike.huadongexcel.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Controller
@CrossOrigin
@Api("表格接口")
@RequestMapping("/api")
public class ExcelController {

    @Autowired
    private ExcelService excelService;

    @PostMapping("login")
    @ApiOperation("登录")
    @PassToken
    @ResponseBody
    public BaseResult login(@RequestBody LoginDto loginDto){
        try {
            LoginVo login = excelService.login(loginDto);
            return RetResponse.makeOKRsp(login);
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }

    }


    @GetMapping("myProject")
    @ApiOperation("我的项目")
    @ResponseBody
    @ApiImplicitParams({ @ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token标记", required = true) })
    public BaseResult myProject(){
        try {
          List<ProjectListVo> list =  excelService.myProject();
            return RetResponse.makeOKRsp(list);
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }

    }

    @PostMapping("addProject")
    @ApiOperation("添加项目")
    @ResponseBody
    @ApiImplicitParams({ @ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token标记", required = true) })
    public BaseResult addProject(@RequestBody AddProjectDto addProjectDto){
        try {
             excelService.addProject(addProjectDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }

    }
    @DeleteMapping("delProject")
    @ApiOperation("删除项目")
    @ResponseBody
    @ApiImplicitParams({ @ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token标记", required = true) })
    public BaseResult delProject( @RequestBody DelProjectDto delProjectDto){
        try {
            excelService.delProject(delProjectDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }

    }

    @GetMapping("userList")
    @ApiImplicitParams({ @ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token标记", required = true) })
    @ApiOperation("员工列表 不包含本人")
    @ResponseBody
    public BaseResult userList(Integer projectId){
        try {
            List<UserVo> userVos = excelService.userList(projectId);
            return RetResponse.makeOKRsp(userVos);
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }

    }

    @GetMapping("dbFileList")
    @ApiImplicitParams({ @ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token标记", required = true) })
    @ApiOperation("数据库列表")
    @ResponseBody
    public BaseResult DBFileList(Integer projectId){
        try {
            List<DBFileListVo> dbFileListVos = excelService.DBFileList(projectId);
            return RetResponse.makeOKRsp(dbFileListVos);
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }

    }

    @GetMapping("DBFileDownLoad")
    @ApiOperation("文件模板下载")
    @ResponseBody
    @ApiImplicitParams({ @ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token标记", required = true) })
    public void DBFileDownLoad(String filePath,String fileName, HttpServletResponse response) throws FileNotFoundException {
            excelService.DBFileDownLoad(filePath,fileName,response);

    }

    @PostMapping("DBFileUpLoad")
    @ApiOperation("文件模板上传")
    @ResponseBody
    @ApiImplicitParams({ @ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token标记", required = true) })
    public BaseResult DBFileUpLoad(MultipartFile file, Integer projectId){
        try {
            excelService.DBFileUpLoad(file,projectId);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }

    }

    @GetMapping("resultList")
    @ApiOperation("结果文件列表")
    @ResponseBody
    @ApiImplicitParams({ @ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token标记", required = true) })
    public BaseResult resultList(Integer projectId){
        try {
            List<ResultListVo> re = excelService.resultList(projectId);
            return RetResponse.makeOKRsp(re);
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }

    }

    @GetMapping("resultDownLoad")
    @ApiOperation("结果文件下载")
    @ResponseBody
    @ApiImplicitParams({ @ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token标记", required = true) })
    public void resultDownLoad(String filePath,String fileName, HttpServletResponse response) throws IOException {
            excelService.resultDownLoad(filePath,fileName,response);

    }

    @PostMapping("resultSave")
    @ApiOperation("结果文件暂存")
    @ResponseBody
    @ApiImplicitParams({ @ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token标记", required = true) })
//    public BaseResult resultSave(String filePath,String fileName, HttpServletResponse response){
    public BaseResult resultSave(@RequestBody ResultSave resultSave){
        try {
            excelService.resultSave(resultSave);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }

    }

}
